package com.jt.www.common.controller;


import com.jt.www.annotations.AvoidRepeatableCommit;
import com.jt.www.common.service.SchoolService;
import com.jt.www.model.reps.GenericDataResponse;
import com.jt.www.model.reps.ResultDTO;
import com.jt.www.model.school.School;
import com.jt.www.model.school.vo.SchoolVo;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@Api(value = "学校信息接口", tags = {"SCHOOL-INFO-API"}, description = "学校信息接口")
@RestController
@RequestMapping(value = "/schoolinfo")
public class SchoolController {

    public static final Logger log = LoggerFactory.getLogger(SchoolController.class);

    @Autowired
    private SchoolService schoolService;

    /**
     * 新增学校信息
     * msg设置为空
     */
    @ApiImplicitParams({@ApiImplicitParam(paramType = "header", dataType = "String", name = "X-RESUB-TOKEN", value = "token", required = true)})
    @AvoidRepeatableCommit
    @ApiOperation(value = "新增信息", notes = "新增信息", httpMethod = "POST")
    @PostMapping("/addSchoolInfo")
    public ResponseEntity insertSchoolInfo(@RequestBody School school) {


        Integer num = schoolService.insertSchoolInfo(school);
        if (num == 1) {
            return GenericDataResponse.ok();
        } else if (num == -1) {
            return GenericDataResponse.errorWithMsg("");//参数传入不正确,学校名、省、市、区编码不能为空
        } else if (num == -2) {
            return GenericDataResponse.errorWithMsg("");//学校名已存在不能重复添加
        }
        return GenericDataResponse.errorWithMsg("");//添加失败
    }

    /**
     * 根据学校名称、学校地址、联系人姓名、联系人电话查询学校信息
     */
    @ApiOperation(value = "根据学校名称、学校地址、联系人姓名、联系人电话、省、市、区查询学校信息", notes = "根据学校名称、学校地址、联系人姓名、联系人电话、省、市、区查询学校信息", httpMethod = "GET")
    @GetMapping(value = "/getSchoolByNameAddressPhone")
    public ResultDTO searchSchoolByNameAddressPhone(SchoolVo school) {
        ResultDTO resultDTO = schoolService.searchSchoolByNameAddressPhone(school);
        return resultDTO;
    }

    /**
     * 删除
     */
    @ApiOperation(value = "学校删除消息", notes = "学校删除消息", httpMethod = "GET")
    @GetMapping(value = "/deleteSchool")
    public ResponseEntity deleteSchoolById(@ApiParam(value = "消息id", required = true) @RequestParam("id") String id) {

        if (StringUtils.isEmpty(id)) {
            log.error("【删除消息】id为空");
        }

        int num = schoolService.deleteSchoolById(Integer.valueOf(id));
        if (num == 1) {
            return GenericDataResponse.ok();
        }
        return GenericDataResponse.errorWithMsg("修改失败");
    }

    /**
     * 修改学校信息
     */
    @ApiOperation(value = "修改学校信息", notes = "修改学校信息", httpMethod = "POST")
    @PostMapping(value = "/updateSchool")
    public ResponseEntity updateSchoolInfo(@RequestBody School school) {

        Integer num = schoolService.updateSchoolInfo(school);
        if (num == 1) {
            return GenericDataResponse.ok();
        } else if (num == -1) {
            return GenericDataResponse.errorWithMsg("参数不能为空");
        }
        return GenericDataResponse.errorWithMsg("修改失败");
    }
}
